<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Register</title>
    <link rel="stylesheet" th:href="@{/css/style.css}" />
</head>
<body>
<header>
    <div class="container">
        <div id="branding">
            <h1>图书管理系统注册界面</h1>
        </div>
        <nav>
            <ul>
                <li><a th:href="@{/}">主页</a></li>
                <li><a th:href="@{/login}">登录</a></li>
            </ul>
        </nav>
    </div>
</header>
<div class="container">
    <form th:action="@{/register}" method="post" enctype="multipart/form-data">
        <div><label>用户名: <input type="text" name="username" required></label></div>
        <div><label>密码: <input type="password" name="password" required></label></div>
        <div><label>邮箱: <input type="email" name="email" required></label></div>
        <div><label>角色: <input type="text" name="role" value="USER" required></label></div>
        <div>
            <label for="avatar">头像:</label>
            <input type="file" id="avatar" name="avatar" accept="image/*" />
            <div id="avatarPreview"></div>
        </div>
        <div><input type="submit" value="注册"></div>
    </form>
</div>

<script>
    document.getElementById('avatar').addEventListener('change', function(event) {
        const file = event.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                const previewDiv = document.getElementById('avatarPreview');
                if (previewDiv.querySelector('img')) {
                    previewDiv.querySelector('img').src = e.target.result;
                } else {
                    const img = document.createElement('img');
                    img.src = e.target.result;
                    img.alt = "头像预览";
                    img.width = 100;
                    img.height = 100;
                    previewDiv.appendChild(img);
                }
            };
            reader.readAsDataURL(file);
        }
    });
</script>
</body>
</html>